<?php

namespace app\common\service;

class CategoryService
{
    /**
     * 递归获取树形结构
     * @param     $list
     * @param int $pid
     * @return array
     */
    public function getTreeData($list, int $pid = 0): array
    {
        $data = [];
        foreach($list as $key => $item) {
            if($item['pid'] == $pid) {
                $children = $this->getTreeData($list, $item['id']);
                !empty($children) && $item['children'] = $children;
                $data[] = $item;
                unset($list[$key]);
            }
        }
        return $data;
    }
}